package com.freak.freakmusic.view.load

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.freak.freakmusic.R


/**
 * @author freak
 * @date 2019/9/11.
 */

class LoadingViewDialog : DialogFragment() {

    private var mAVLoadingIndicatorView: AVLoadingIndicatorView? = null
    private var mTextViewTitle: TextView? = null

    private var mTitle: String? = null

    private var isShow: Boolean = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onStart() {
        super.onStart()
        val window = dialog!!.window
        if (window != null) {
            val layoutParams = window.attributes
            layoutParams.dimAmount = 0.0f
            window.attributes = layoutParams
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        dialog!!.requestWindowFeature(Window.FEATURE_NO_TITLE)
        dialog!!.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        dialog!!.setCanceledOnTouchOutside(false)


        val view = inflater.inflate(R.layout.layout_loading_view, null)
        mAVLoadingIndicatorView = view.findViewById(R.id.av_loading_view)
        mTextViewTitle = view.findViewById(R.id.text_view_loading_title)

        if (!TextUtils.isEmpty(mTitle)) {
            mTextViewTitle!!.text = mTitle
        }
        return view
    }


    fun setTitle(title: String) {
        mTitle = title
    }

    fun show(activity: AppCompatActivity?) {
        if (mAVLoadingIndicatorView != null) {
            if (mAVLoadingIndicatorView!!.visibility == View.VISIBLE) {
                return
            }
            if (mAVLoadingIndicatorView!!.visibility == View.GONE) {
                mAVLoadingIndicatorView!!.visibility = View.VISIBLE
                mAVLoadingIndicatorView!!.show()
            }
        }
        if (!isVisible) {
            isShow = true
            if (activity != null) {
                show(activity.supportFragmentManager, "LoadingViewDialog")
            }

        }
    }

    override fun show(manager: FragmentManager, tag: String?) {
        try {
            //防止连续点击add多个fragment
            manager.beginTransaction().remove(this).commit()
            super.show(manager, tag)
        } catch (e: Exception) {
            e.printStackTrace()
        }

    }

    override fun dismiss() {
        //        if (isShow) {
        //            if (isVisible()) {
        //                if (mAVLoadingIndicatorView != null) {
        //                    if (mAVLoadingIndicatorView.getVisibility() == View.VISIBLE) {
        //                        mAVLoadingIndicatorView.setVisibility(View.GONE);
        //                    }
        //                    mAVLoadingIndicatorView.hide();
        //                }
        //                if (isShow) {
        //                    isShow = false;
        ////                    if (mViewDialog != null && mViewDialog.getDialog() != null && mViewDialog.getDialog().isShowing()){
        ////                        mViewDialog.dismissAllowingStateLoss();
        ////                    }
        //                        super.dismiss();
        //                }
        //
        //
        //            } else {
        //                if (isShow) {
        //                    isShow = false;
        ////                    if (mViewDialog != null && mViewDialog.getDialog() != null && mViewDialog.getDialog().isShowing()){
        ////                        mViewDialog.dismissAllowingStateLoss();
        ////                    }
        //                    super.dismiss();
        //                }
        //            }
        //        }
        if (mViewDialog != null && mViewDialog!!.dialog != null && mViewDialog!!.dialog!!.isShowing) {
            mViewDialog!!.dismissAllowingStateLoss()
        }
    }

    override fun onDetach() {
        super.onDetach()
        isShow = false
    }

    override fun onDestroyView() {
        if (mAVLoadingIndicatorView != null) {
            mAVLoadingIndicatorView!!.hide()
        }
        isShow = false
        super.onDestroyView()
    }

    companion object {
        private var mViewDialog: LoadingViewDialog? = null


        val instance: LoadingViewDialog
            get() {
                if (mViewDialog == null) {
                    mViewDialog = LoadingViewDialog()
                }
                return mViewDialog as LoadingViewDialog
            }
    }
}
